import javax.naming.InsufficientResourcesException;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28779
 * Date: 2023-04-20
 * Time: 15:07
 */
public class MyStack {
    private Queue<Integer> qu1;
    private Queue<Integer> qu2;
    public MyStack() {
        qu1 = new LinkedList<>();
        qu2 = new LinkedList<>();
    }

    public void push(int x) {
        if(qu1.isEmpty()) {
            qu1.offer(x);
        } else if (qu2.isEmpty()) {
            qu2.offer(x);
        } else {
            qu1.offer(x);
        }
    }

    public int pop() {
        if(empty()) {
            return -1;
        }
        if(!qu1.isEmpty()) {
            int temp = qu1.size();
            for(int i = 0; i < temp - 1; i++) {
                int temp2 = qu1.poll();
                qu2.offer(temp2);
            }
            return qu1.poll();
        }
        if(!qu2.isEmpty()) {
            int temp = qu2.size();
            for (int i = 0; i < temp - 1; i++) {
                int temp2 = qu2.poll();
                qu1.offer(temp2);
            }
            return qu2.poll();
        }
        return -1;
    }

    public int top() {
        if(empty()) {
            return -1;
        }
        if(!qu1.isEmpty()) {
            int temp = qu1.size();
            int temp2 = -1;
            for(int i = 0; i < temp; i++) {
                temp2 = qu1.poll();
                qu2.offer(temp2);
            }
            return temp2;
        }
        if(!qu2.isEmpty()) {
            int temp = qu2.size();
            int temp2 = -1;
            for (int i = 0; i < temp; i++) {
                temp2 = qu2.poll();
                qu1.offer(temp2);
            }
            return temp2;
        }
        return -1;
    }

    public boolean empty() {
        return qu2.isEmpty() && qu1.isEmpty();
    }
}
